package com.hua.yuan.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hua.yuan.R;
import com.hua.yuan.ui.module.ImageListBean;

import java.util.List;

/**
 * Created by huangyang on 2018/5/9.
 * 图集列表
 */

public class TujiListAdapter extends BaseAdapter {

    private Context context;

    public TujiListAdapter(Context context) {
        this.context = context;
    }

    private List<ImageListBean> list;

    public List<ImageListBean> getList() {
        return list;
    }

    public void setList(List<ImageListBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        return list == null ? null : list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return list == null ? 0 : i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.show_video_item_view, null);
            viewHolder = new ViewHolder(view);

            view.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.bind(i);

        return view;
    }

    class ViewHolder {


        private ImageView itemImage;
        private TextView itemName;
        private TextView itemDis;
        private TextView itemType;
        private TextView itemJf;
        private TextView itemJF1;
        private TextView tv_sees_hint;
        private TextView itemPlayCount;
        private ImageView itemShowHy;


        ViewHolder(View item) {

            itemImage = item.findViewById(R.id.itemImage);

            itemName = item.findViewById(R.id.itemName);
            itemDis = item.findViewById(R.id.itemDis);
            itemType = item.findViewById(R.id.itemType);
            itemJf = item.findViewById(R.id.itemJf);
            itemJF1 = item.findViewById(R.id.itemJF1);
            tv_sees_hint = item.findViewById(R.id.tv_sees_hint);
            itemPlayCount = item.findViewById(R.id.itemPlayCount);

            itemShowHy = item.findViewById(R.id.itemShowHy);

        }

        @SuppressLint("SetTextI18n")
        void bind(int i) {

            itemJF1.setVisibility(View.GONE);
            itemShowHy.setVisibility(View.GONE);

            ImageListBean bean = list.get(i);

            //显示图片的地址
            Glide.with(context)
                    .load(bean.getImg_url())
                    .into(itemImage);

            //显示名字
            itemName.setText(bean.getTitle());

            //显示描述
            itemDis.setText(bean.getAtlas_desc());

            //显示类型
            itemType.setText("分类：" + bean.getClassifyDesc());

//            if ("1".equals(bean.getIs_free())) {
//                itemJf.setVisibility(View.INVISIBLE);
//            } else {
//                itemJf.setVisibility(View.VISIBLE);
//            }

            //显示积分
            itemJf.setText(String.valueOf(bean.getNow_price()) + "代币");

            //显示播放次数
            int count;
            try {
                count = Integer.parseInt(bean.getPlay_num());
            } catch (NumberFormatException e) {
                count = 0;
            }
            if (count > 999) {
                itemPlayCount.setText("999+");
            } else {
                itemPlayCount.setText(String.valueOf(count));
            }
            tv_sees_hint.setText("观看：");
        }
    }
}
